<?php

class BrowseController extends Controller {
	
	public $layout = 'application.views.layouts.main';
	
	public function actionIndex() {
		$this->layout = 'application.views.layouts.main2';
		
		$this->render ( 'index' );
	}
	
	public function actionSearch() {
		
		$this->layout = 'application.views.layouts.main2';
		if (! empty ( $_REQUEST ) && isset( $_REQUEST['keyword'])) {
			
			$keyword = $_REQUEST['keyword'];
			$criteria = new EMongoCriteria ();
			
			$regex = new MongoRegex ( "/{$keyword}/i" ) ;
			$criteria->setConditions ( 
					array ('$or' => array (array ('name' => $regex ), 
									array ('title' => $regex ),
									array ('intro' => $regex),

									) ) ); //,'$options'=>'i'
//			print_r($criteria->getConditions());
			
			$page = Page::model ()->findAll ( $criteria );
			
			$this->render ( 'search' ,compact('pages'));
		}
	}
	public function actionRandom() {
		
		if (! isset ( Yii::app ()->user->mmUserId )) {
			$max_min_user_id_dat = Yii::getPathOfAlias ( "application.runtime.max_min_user_id_dat" );
			@unlink ( $max_min_user_id_dat );
			if (! file_exists ( $max_min_user_id_dat ) || (time () - filectime ( $max_min_user_id_dat )) > 60 * 60 * 24) {
				@unlink ( $max_min_user_id_dat );
				$max_min_user_id = array ();
				$criteria = new EMongoCriteria ();
//				$criteria->sort ( 'muid', - 1 );
//				$criteria->setSelect ( array ('muid' ) );
				$user = User::model ()->findAll ( $criteria );
				
				if ($user) {
					$max_min_user_id ['max'] = $user [0]->muid;
				}
				$criteria = new EMongoCriteria ();
				
				$criteria->sort ( 'muid', 1 );
				$criteria->setSelect ( array ('muid' ) );
				
				$user1 = User::model ()->findAll ( $criteria );
				if ($user1) {
					$max_min_user_id ['min'] = $user1 [0]->muid;
				}
				
				file_put_contents ( $max_min_user_id_dat, serialize ( $max_min_user_id ) );
			}
			$max_min_user_id = unserialize ( file_get_contents ( $max_min_user_id_dat ) );
			Yii::app ()->user->setState ( 'mmUserId', $max_min_user_id );
		}
		$mmUserId = Yii::app ()->user->mmUserId;
		$muid = mt_rand ( $mmUserId ['min'], $mmUserId ['max'] );
		
		$criteria = new EMongoCriteria ();
		$criteria->muid ( '>=', $muid );
		
		$user = User::model ()->find ( $criteria );
		if ($user == null) {
			$criteria = new EMongoCriteria ();
			$criteria->muid ( '<=', $muid );
			
			$user = User::model ()->find ( $criteria );
		
		}
		
		$page = Page::model ()->findByMuid ( $user->_id );
		$this->render ( 'random', array ('page' => $page ) );
	}
	
// -----------------------------------------------------------
// Uncomment the following methods and override them if needed
/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}